// pages/visitor_submit_List/index.js
const appInstance = getApp()
const uploadUrl = appInstance.globalData.request_url + 'factory/upload_files'
const uploadPromises = [];
import ActionSheet from 'tdesign-miniprogram/action-sheet/index';
import drawQrcode from 'weapp-qrcode'

Component({
  data: {
    last_select_unit:null,
    dateText: '',
    dateValue: [],
    unit_list: [],
    result: [],
    options: [
    ],
    reason_for_type:"visitor",
    pop_visible:false,
    checked:false,
    query_respondent_list:[],
    checkAllValues: {},
    respondent_list:[],
    verify_loading_visible:false,
    loading_visible:false,
    overlay_visible:false,
    activeValues: [0],
    send_disabled:false,
    send_result:false,
    cur: {},
    position: [
      { value: 'top', text: '顶部弹出' },
      { value: 'left', text: '左侧弹出' },
      { value: 'center', text: '中间弹出' },
      { value: 'bottom', text: '底部弹出' },
      { value: 'right', text: '右侧弹出' },
    ],
    visible:false,
    product: {
      value: 'all',
    },
    showIndex: false,
    closeBtn: false,
    deleteBtn: false,
  },
  methods: {
    generateUUID() {
      const timestamp = Date.now().toString(16);
      const randomStr = Math.random().toString(16).substring(2, 10);
      return `wx-${timestamp}-${randomStr}-${this.generateRandomBlock()}-${this.generateRandomBlock()}`;
    },    
    generateRandomBlock() {
      return Math.random().toString(16).substring(2, 10);
    },

    setuuid(){
      // 示例：生成 UUID
      const uuid = this.generateUUID();
      console.log('生成uuid' + uuid)
      this.setData({
        uuid:uuid
      })
    },
    onLoad(options){
      this.setuuid()
    },
    test_btn(){
      wx.chooseMedia({
        count: 9,
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          console.log(res.tempFiles[0].tempFilePath)
          console.log(res.tempFiles[0].size)
        }
      })
    },
    test_btn2(){
      const fs = wx.getFileSystemManager()
fs.readFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  encoding: 'utf8',
  position: 0,
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.error(res)
  }
})
    },
    onColumnChange(e) {
      console.log('picker pick:', e);
      if (e.detail.value.length){
        let current_unit_value = e.detail.value[0]
        if(e.detail.column === 0) {
          this.data.result.forEach((item, index) => {
            if (current_unit_value == item.value ){
              this.setData({address_list:item.address_list})
              // this.setData({address_list:item.address_list})
              // if (current_unit_value != this.data.last_select_unit){
              //   // 更新  // 赋值
              //   // console.log(item)
              //   // console.log(current_unit_value)
              //   // console.log('赋值id' + item)
              //   this.setData({address_list:item.address_list})
              //   // this.setData({address_list:item.address_list,last_select_unit:current_unit_value})
              // }
            }
        })
        }
        
        
      }
    },
    handleOpenPrivacyContract(){
      wx.openPrivacyContract({
        success: () => {console.log(1)}, // 打开成功
        fail: () => {console.log(2)}, // 打开失败
        complete: () => {console.log(3)}
      })
    },
    handleAgreePrivacyAuthorization(){
      console.log(11111)
    },

    onPickerChange(e) {
      const { key } = e.currentTarget.dataset;
      const { value } = e.detail;
      const { label } = e.detail;
      console.log('picker change:', e.detail);
      this.setData({
        [`${key}Visible`]: false,
        [`${key}Value`]: value,
        [`${key}Text`]: label.join(' '),
      });
      this.visitor_search_respondent()
    },

    onPickerCancel(e) {
      const { key } = e.currentTarget.dataset;
      console.log(e, '取消');
      console.log('picker1 cancel:');
      this.setData({
        [`${key}Visible`]: false,
      });
    },
    onClickPicker(e) {
      console.log(e)
      this.setData({dateVisible: true})
      // address_list:this.data.result[0].address_list,result:this.data.result
    },
    clear_respondent_address_list(e){
      let respondent_address_list = this.data.respondent_address_list
      respondent_address_list.forEach((item, index) => {
        item.checked = false
      })
      this.setData({pop_visible_address:false,respondent_address_list:respondent_address_list })

    },
    changeAddress(e){
      console.log(e)
      let respondent_address_list = this.data.respondent_address_list
      let current_select_address = e.detail.value
      if (this.data.current_select_address && current_select_address !=this.data.current_select_address ){
        respondent_address_list.forEach((item, index) => {
          if (item.address_name == current_select_address) {
              item.checked  = true
          }else{
            item.checked  = false
          }
        })
        this.setData({current_select_address:this.data.current_select_address,respondent_address_list:respondent_address_list,display_select_button:true})
      }else if (this.data.current_select_address && current_select_address ==this.data.current_select_address)
      {
        console.log(222)
        respondent_address_list.forEach((item, index) => {
          if (item.address_name == current_select_address) {
              item.checked  = false
          }
        })
        this.setData({current_select_address:null, respondent_address_list:respondent_address_list,display_select_button:false})
      }
      else{
        console.log(333)
        respondent_address_list.forEach((item, index) => {
          if (item.address_name == current_select_address) {
              item.checked  = true
          }else{
            item.checked  = false
          }
        })
        this.setData({current_select_address:current_select_address, respondent_address_list:respondent_address_list,display_select_button:true})
      }
    },
    get_default_unit(e){
      let that = this
      this.data.respondent_address_list.forEach((item, index) => {
        if (item.unit_name == e.detail.value && item.default) {
          that.setData({respondent_unit:item.unit_name, respondent_address:item.address_name})
        }
      })
    },
    popUpAddress(){
      this.setData({pop_visible_address:true})
    },
    showendPicker(e) {
      this.setData({
        data_assembly_end_visible: true,
      });
    },
    onConfirmend(e) {
      const { value } = e.detail;
      this.setData({
        expected_visit_date: value,
      });
      this.hidePicker();
    },
    hidePicker() {
      this.setData({
        visible: false,
      });
    },
    onChangeSearchValue(e){
      console.log(e)
      if (!e.detail.value){
        this.setData({
          options:[],
        });
      }
      this.setData({
        search_content:e.detail.value
      });
      this.visitor_search_respondent()
    },
    sleep(ms, callback) {
      setTimeout(callback, ms);
    },
    clearSearchValue(e){
      let that = this
      this.setData({
        search_content:null,
        options:[],
        display_confirm_button:false,
      });
      // this.data.search_content = ""
      that.sleep(1000, () => {
        this.visitor_search_respondent()
    })
    },
    findQueryByItem(array, value) {
      // return array.find(obj => obj === value);
    return  array.some(item => {
        return JSON.stringify(item) === JSON.stringify(value)
      })
    },
    findCheckByValue(array, value) {
    return  array.some(item => {
        return item === value
      })
    },
    confirmSearchResult(e){
      this.clearSearchValue(e)
      this.setData({
        display_confirm_button:false
      })
      let that = this
      let query_respondent_list = that.data.query_respondent_list
      let checkAllValues = that.data.checkAllValues.value
      wx.login({
        success: (res) => {
          wx.request({
            url: appInstance.globalData.request_url + "factory/query_respondent",
            method:"GET",
            data:{
              "code":res.code,
              "query_respondent_list": checkAllValues,
            },
            header:{
              'content-type':null
            },
            success (resp) {
              if(resp.statusCode==200){
                if(resp.data.status == 200){
                  if (query_respondent_list.length) {
                    console.log(1111)
                    resp.data.data.forEach((item, index) => {
                      if (query_respondent_list.length) {
                        if (!that.findQueryByItem(query_respondent_list,item))
                        query_respondent_list.push(item)
                        that.setData({query_respondent_list:query_respondent_list,pop_visible:false,options:[],checkAllValues:{}})
                      }
                    })
                  }else{
                    console.log(2222)
                    query_respondent_list = resp.data.data
                    that.setData({query_respondent_list:query_respondent_list,pop_visible:false,options:[],checkAllValues:{}})
                  } 
              
                  // that.setData({query_respondent_list:query_respondent_list,pop_visible:false,options:[]})
                }else{
                  console.log(333)
                  that.setData({query_respondent_list:that.data.query_respondent_list,pop_visible:false,options:[],checkAllValues:{}})
                }
              }
            },
            fail (err){
              console.log(444)
              that.setData({options:[],pop_visible:false,checkAllValues:{}})
              wx.showToast({
                title: err,
                icon: 'error',
                duration: 2000
              })
            }
          })
        },
        fail(err){
          that.setData({options:[],checkAllValues:{}})
          console.log(err)
        }
      })
   
    },
    visitor_search_respondent(){
      let that = this
      wx.login({
        success: (res) => {
          wx.request({
            url: appInstance.globalData.request_url + "factory/search_respondent",
            method:"GET",
            data:{
              "code":res.code,
              "search_content":that.data.search_content ? that.data.search_content : 'null' ,
              "search_unit":that.data.dateValue
            },
            header:{
              'content-type':null
            },
            success (resp) {
              console.log(resp)
              if(resp.statusCode==200){
                if(resp.data.status == 200){
                  that.setData({options:resp.data.data})
                }else{
                  that.setData({options:[]})
                }
              }
            },
            fail (err){
              that.setData({options:[]})
              wx.showToast({
                title: err,
                icon: 'error',
                duration: 2000
              })
            }
          })
        },
        fail(err){
          that.setData({options:[]})
          console.log(err)
        }
      })
    },
    displayPop(){
      this.setData({pop_visible:true})
    },
    onVisibleChangePop(){
      this.setData({pop_visible:false})
    },
    isEmptyObject(obj) {
      return Object.keys(obj).length === 0 && obj.constructor === Object;
    },
    onCheckAllChange(event) {
      console.log(event)
      this.setData({
        display_confirm_button:true
      })
      let enent_value = event.detail.value
      let enent_context_value = event.detail.context.value
      let checkAllValues = this.data.checkAllValues;
      let value = checkAllValues.value?checkAllValues.value:[]
      if (enent_value.length){
        if (value.length){
          if (!this.findCheckByValue(enent_value, enent_context_value)){
            value = enent_value.filter(item => item !== enent_context_value)
          }else{
            value = value.concat(enent_value)
          }
        }else{
          value = enent_value
        }
      }else{
        if (this.findCheckByValue(value, enent_context_value)){
          value = value.filter(item => item !== enent_context_value)
        }
      }
      value = [...new Set(value)]
      checkAllValues.value = value
      this.setData({
        checkAllValues: checkAllValues,
      });
    },
    send_phone_verify_code(e){
      this.setData({send_disabled:true, verify_loading_visible:true, text:'发送中,验证码有效期5分钟..'})
      let that = this
      wx.login({
        success: (res) => {
          this.setData({send_disabled:false})
          wx.request({
            url: appInstance.globalData.request_url + "factory/send_phone_verify_code",
            method:"POST",
            data:{
              "wx_login_code":res.code,
              "phone_number":that.data.update_phone_number,
            },
            dataType:"json",
            success (resp) {
              console.log(resp)
              that.setData({verify_loading_visible:false})
              if(resp.statusCode==200){
                if(resp.data.result.status_code==200){
                  that.setData({send_result:true, msg:resp.data.result.msg,sms_log_id:resp.data.result.sms_log_id})
                }else{
                  that.setData({send_result:false,msg:resp.data.result.msg})
                }
              }else{
                wx.showToast({
                  title: '服务器错误',
                  icon: 'error',
                  duration: 2000
                })
              }
            },
            fail (err){
              console.log('失败')
              that.setData({verify_loading_visible:false})
              wx.showToast({
                title: resp.data,
                icon: 'error',
                duration: 2000
              })
            }
          })
        },
        fail(err){
          this.setData({send_disabled:false,verify_loading_visible:false})
          console.log(err)
        }
      })
    },
    cancel_visitor_coding_submit(e){
      this.setData({visible:false,send_result:false,msg:null})
    },
    display_attachments(e){
      this.setData({upload_visible:true})
    },
    delete_attachment(e){
      console.log(e)
      let tempFilePaths = this.data.tempFilePaths
      let delete_index = e.currentTarget.dataset.id
      tempFilePaths.forEach((item,index) => {
        if (index == delete_index){
          tempFilePaths.splice(delete_index, 1)
          this.setData({tempFilePaths:tempFilePaths})
        }
      })
    },
    //图片预览
    onChange(e) {
      const { index } = e.detail;
      console.log(index);
    },

    onDelete(e) {
      let query_respondent_list = this.data.query_respondent_list
      console.log(e)
      let delete_id = e.currentTarget.dataset.id
      query_respondent_list = query_respondent_list.filter(item => item.value !== delete_id)
      this.setData({query_respondent_list:query_respondent_list})
    },
    cancelSearchValue(e){
      this.setData({pop_visible:false, pop_visible_address:false, checkAllValues:{},search_content:null,})
    },
    onClose(e) {
      const { trigger } = e.detail;
      console.log(trigger);
      this.setData({
        upload_visible: false,
      });
    },
    filter_file(){
      let filter_no_upload_file_list = []
      let filter_has_upload_file_list = []
      this.data.tempFilePaths.forEach((item) => {
        if (!item.has_upload){
          filter_no_upload_file_list.push(item)
        }else{
          filter_has_upload_file_list.push(item)
        }
      })
      return {'filter_no_upload_file_list':filter_no_upload_file_list,'filter_has_upload_file_list':filter_has_upload_file_list }
    },


    upload_file(header, visitor_id){
      let filter_file = this.filter_file()
      filter_file.filter_no_upload_file_list.forEach((file) => {
        const uploadPromise = new Promise((resolve, reject) => {
          wx.uploadFile({
            url: uploadUrl,
            filePath: file.path,
            formData:{
              'file_name':file.name,
              'visitor_id':visitor_id?visitor_id:null,
            },
            header:{
              'Cookie': header
            },
            name: 'file',
            success: (res) => {
              resolve(res.data);
            },
            fail: (error) => {
              reject(error);
            }
          });
        });
        // 将每个上传请求Promise添加到数组中
        uploadPromises.push(uploadPromise);
      });
      // 使用Promise.all并行发送所有上传请求
      Promise.all(uploadPromises)
        .then((responses) => {
          console.log('所有上传请求成功:', responses);
        })
        .catch((errors) => {
          console.error('上传请求中出现错误:', errors);
        });
    },
    filter_file_suffix(tempFilePaths) {
      const white_suffix_file = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.xls', '.xlsx', '.csv', '.ods', '.pdf','.txt', '.doc', '.docx', '.rtf', '.md', '.pdf', '.odt', '.wps','.xlsm','.ppt', '.pptx', '.pps', '.ppsx', '.odp', '.key'];
      let exists = false
      tempFilePaths.forEach((item) => {
        let file_split_list = item.name.split('.')
        let file_suffix = file_split_list[file_split_list.length - 1]
        let target = '.' + file_suffix.toLowerCase()
        exists = white_suffix_file.includes(target); // true
        if (!exists){
          return exists
        }
      })
      return exists
    },
    choose_files(){
      console.log('开始上传')
      let that = this
      var tempFilePaths = that.data.tempFilePaths ?  that.data.tempFilePaths: []
      console.log('临时文件' + tempFilePaths)
      wx.chooseMessageFile({
          count: 10,
          type: 'all',
          success (res) {
            let result = that.filter_file_suffix(res.tempFiles)
            if (!result){
              wx.showModal({
                title: '上传失败',
                content: '只能上传/图片/pdf/表格',
              })
            }else{
              that.setData({tempFilePaths:[...tempFilePaths, ...res.tempFiles],upload_visible:true})
            }
          }
        })
    },
    onVisibleChange(e) {
      console.log(e)
      this.setData({
        upload_visible: false,
      });
    },
    displayOverlay(e) {
      this.setData({overlay_visible:true, loading_visible:true})
    },
    closeOverlay(e) {
      this.setData({overlay_visible:false, loading_visible:false,verify_loading_visible:false, visible:false})
    },
    checkFieldRequired(user_status){
      let that = this
      let visitor_phone = this.data.visitor_phone
      let query_respondent_list = this.data.query_respondent_list
      if (!visitor_phone){
        this.setData({overlay_visible:false, loading_visible:false})
        wx.showToast({
          title: '请填写访客电话号',
          icon: 'error',
          duration: 2000
        })
     
      }else if(!query_respondent_list.length){
        this.setData({overlay_visible:false, loading_visible:false})
        wx.showToast({
          title: '请选择被访人',
          icon: 'error',
          duration: 2000
        })
      }else{
        let header = appInstance.globalData.cookies
        wx.request({
          url: appInstance.globalData.request_url + "factory/submit_verify",
          method:"POST",
          header: {
            'Cookie': header
          },
          data:{
            "visitor_phone":that.data.visitor_phone ? that.data.visitor_phone : null,
            "query_respondent_list": that.data.query_respondent_list ? that.data.query_respondent_list : null ,
            "visitor_name":that.data.visitor_name ? that.data.visitor_name : null,
            "visitor_unit":that.data.visitor_unit ? that.data.visitor_unit : null,
            "number_of_visitor":that.data.number_of_visitor ? that.data.number_of_visitor : null,
            "reason_for_visit":that.data.reason_for_visit ? that.data.reason_for_visit : null,
            "expected_visit_date":that.data.expected_visit_date ? that.data.expected_visit_date : null,
            "respondent_address_id":that.data.dateValue ? that.data.dateValue : null,
            "visitor_identity_card_number":that.data.visitor_identity_card_number ? that.data.visitor_identity_card_number : null,
          },
          dataType:"json",
          success (resp) {
            if(resp.statusCode==200){
              appInstance.globalData.cookies = resp.cookies[0]
              if (resp.data.result.status != 200){
                wx.showToast({
                  title: resp.data.result.msg,
                  icon: 'error',
                })
                that.setData({loading_visible:false,overlay_visible:false,})
              }else{
                that.setData({respondent_user:resp.data.result.user_id})
                if (user_status == 'has_login'){
                  that.visitor_confirm_coding_submit()
                }else{
                  that.setData({visible:true})
                  that.setData({loading_visible:false,overlay_visible:false,visible:true})
                }
              }
            }
          },
          fail(e){
            that.setData({loading_visible:false,overlay_visible:false,})
            wx.showToast({
              title: '请求出错',
              icon: 'error',
            })
          }
    })
      }
    },
    clear_set_data(e){
      // 清空表单部分数据(根据用户体验清楚字段)
      this.setData({tempFilePaths:[],
        query_respondent_list:[],
        visitor_name:null,
        visitor_phone:null,
        visitor_identity_card_number:null,
        visitor_unit:null,
        number_of_visitor:null,
        reason_for_visit:null,
        query_respondent_list:null,
        sms_log_id:null,
        tempFilePaths:null
      })
    },
    // 填单
    visitor_coding_submit(e){
      let that = this
      let user_status = appInstance.globalData.user_status
      console.log(user_status)
      if (user_status == 'has_login'){
        this.setData({overlay_visible:true, loading_visible:true})
        this.checkFieldRequired(user_status)
      }else if (this.data.visible == false & user_status == 'no_login'){
        this.checkFieldRequired()
      }else{
        // 创建用户 并提交单据
        that.setData({verify_loading_visible:true, text:'提交中,稍安勿躁...'})
        console.log(3)
        wx.login({
          success: (res) => {
            wx.request({
              url: appInstance.globalData.request_url + "factory/create_user_and_login",
              method:"POST",
              data:{
                "wechat_user_code":res.code,
                "user_phone":that.data.update_phone_number,
                "user_phone_code":that.data.phone_number_code,
              },
              dataType:"json",
              success (resp) {
                console.log(resp)
                appInstance.globalData.cookies = resp.cookies[0]
                if(resp.statusCode==200){
                  if(resp.data.result.status == 200){
                    appInstance.globalData.cookies = resp.cookies[0]
                    appInstance.globalData.user_status = 'has_login'
                    appInstance.globalData.user_name = resp.data.result.visitor_name
                    that.visitor_confirm_coding_submit(e)
                  }else{
                    that.setData({msg:resp.data.result.msg,verify_loading_visible:false})
                  }
                }
              },
              fail (err){
                that.closeOverlay()
                that.setData({msg:err})
                console.log('失败')
                wx.showToast({
                  title: resp.data,
                  icon: 'error',
                  duration: 2000
                })
              }
            })
          },
          fail(err){
            that.closeOverlay()
            console.log(err)
          }
        })
      }

    },
    visitor_confirm_coding_submit(e){
      var that = this
      let header = appInstance.globalData.cookies
      let filter_file = this.filter_file()
      console.log('获取到的数据')
      wx.login({
        success: (res) => {
          wx.request({
            url: appInstance.globalData.request_url + "factory/visitor_coding_submit",
            method:"POST",
            header: {
              'Cookie': header
            },
            data:{
              "uuid":that.data.uuid,
              "wechat_user_code":res.code,
              "visitor_name":that.data.visitor_name,
              "visitor_phone":that.data.update_phone_number,
              "visitor_identity_card_number":that.data.visitor_identity_card_number,
              "visitor_unit":that.data.visitor_unit,
              "number_of_visitor":that.data.number_of_visitor,
              "reason_for_visit":that.data.reason_for_visit,
              "reason_for_type":that.data.reason_for_type,
              'sms_log_id':that.data.sms_log_id,
              'query_respondent_list':that.data.query_respondent_list,
              'delivery_car_number':that.data.delivery_car_number,
              'visitor_item_list':that.data.visitor_item_list,
              'expected_visit_date':that.data.expected_visit_date,
              'delivery_car_number':that.data.delivery_car_number,
              'detail_for_delivery':that.data.detail_for_delivery,
              'delivery_boards_number':that.data.delivery_boards_number,
              'delivery_volume':that.data.delivery_volume,
              'respondent_address_id':that.data.dateValue,
              // 'filter_has_upload_file_list':filter_file.filter_has_upload_file_list
              // "respondent_qw_wechat_number":that.data.respondent_qw_wechat_number,
            },
            dataType:"json",
            success (resp) {
              console.log(resp)
              that.closeOverlay()
              // 防止拿到其他访客的session登录预览数据
              // appInstance.globalData.cookies = resp.cookies[0]
              if(resp.statusCode==200){
                // appInstance.globalData.cookies = resp.cookies[0]
                if (resp.data.result.status != 200){
                  wx.showToast({
                    title: resp.data.result.msg,
                    icon: 'error',
                  })
                  that.setData({loading_visible:false,overlay_visible:false,})
                }else{
                  //上传附件
                  let visitor_id = resp.data.result.visitor_id
                  if (that.data.tempFilePaths){
                    that.upload_file(resp.cookies[0], visitor_id)
                  }
                  that.setData({loading_visible:false,overlay_visible:false,tempFilePaths:[],query_respondent_list:[],uuid:null})
                  that.setuuid()
                  wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                  })
                  wx.switchTab({
                    url: "/pages/visitor_submit_list/index"
                  })
                  if(resp.data.result.code==403){
                    that.setData({loading_visible:false,overlay_visible:false,})
                    wx.showToast({
                      title: resp.data,
                      icon: 'error',
                      duration: 2000
                    })
                  }
                }
              }
            },
            fail(e){
              that.closeOverlay()
              console.log('小程序登录失败' + e)
            }
      })
        }
      })
    },
    redirect_web_form(){
      // let src_url = appInstance.globalData.request_url + 'upload/file' 
      let src_url = 'https://sso.zuru.cloud/' + 'upload/file'
      wx.navigateTo({
       url: "/pages/form/index?src_url=" + src_url  + '&uuid=' + this.data.uuid
     })
    },
    onHide(){
      console.log('触发清除！！')
      // this.clear_set_data()
    },
    handleChange(e) {
      let that = this
      console.log(e)
      let user_auth_result = e.currentTarget.dataset.value
      if (user_auth_result){
        wx.requestSubscribeMessage({
          tmplIds: ['dKfvgYDN8uwWQj8a51zg84THmdj2q-9ONVZijrtFR7w'],
          success (res) {
            console.log(res)
            that.setData({subscribe_result:res, defaultVal:true})
           },
           fail(err){
             console.log('用户未订阅')
             console.log(err)
           },
           complete(){
             that.visitor_coding_submit()
           }
        })
      }else{
        that.setData({defaultVal:false, subscribe_result:null})
      }
    },
    getRespondent_address_list(e){
      let that = this
      return new Promise(function (resolve, reject){
        wx.login({
          success: (res) => {
            wx.request({
              url: appInstance.globalData.request_url + "factory/search_respondent_address",
              method:"GET",
              data:{
                "code":res.code,
              },
              header:{
                'content-type':null
              },
              success (resp) {
                if(resp.statusCode==200){
                  if(resp.data.status == 200){
                    that.setData({result:resp.data.result, address_list:resp.data.result[0].address_list,choose_start_time:resp.data.choose_start_time})
                  }
                }
                resolve(resp.data.result)
              },
              fail (err){
                reject(err)
              }
            })
          },
          fail(err){
            reject(err)
          }
        })
      })
    },


    onShow(){
      let that = this
      console.log(appInstance.globalData.file_data)
      let tempFilePaths =  this.data.tempFilePaths ? this.data.tempFilePaths : []
      if (appInstance.globalData.file_data){
        tempFilePaths = [...tempFilePaths, ...appInstance.globalData.file_data]
        appInstance.globalData.file_data = null
        that.setData({upload_visible:true})
      }
      this.setData({
        tempFilePaths:tempFilePaths
      })
      this.getRespondent_address_list()
    },
    onReady(){
      console.log('重新加载')
    },
    // 获取填单数据
    get_user_card_number(e){
      this.setData({visitor_identity_card_number:e.detail.value})
    },
    get_visitor_name(e){
      this.setData({visitor_name:e.detail.value})
    },
    get_visitor_unit(e){
      this.setData({visitor_unit:e.detail.value})
    },
    get_reason_for_type(e){
      this.setData({reason_for_type:e.detail.value})
    },
    get_expected_visit_date(e){
      this.setData({expected_visit_date:e.detail.value})
    },
    get_delivery_car_number(e){
      this.setData({delivery_car_number:e.detail.value})
    },
    get_visitor_item_list(e){
      this.setData({visitor_item_list:e.detail.value})
    },
    get_detail_for_delivery(e){
      this.setData({detail_for_delivery:e.detail.value})
    },
    get_delivery_boards_number(e){
      this.setData({delivery_boards_number:e.detail.value})
    },
    get_delivery_volume(e){
      this.setData({delivery_volume:e.detail.value})
    },
    get_visitor_phone_number(e){
      this.setData({visitor_phone:e.detail.value,update_phone_number:e.detail.value})
    },
    get_visitor_num(e){
      this.setData({number_of_visitor:e.detail.value})
    },
    get_visitor_reason(e){
      this.setData({reason_for_visit:e.detail.value})
    },
    get_respondent_name(e){
      this.setData({respondent_or_consignee:e.detail.value})
    },
    get_respondent_phone_number(e){
      this.setData({respondent_phone_number:e.detail.value})
    },
    get_respondent_qw_wechat_number(e){
      this.setData({respondent_qw_wechat_number:e.detail.value})
    },
    get_phone_number_code(e){
      this.setData({phone_number_code:e.detail.value})
    },
    update_phone_number(e){
      this.setData({update_phone_number:e.detail.value})
    }
  }
})
